#!/bin/bash

CUR_DIR=`pwd`




function lcov_isexits()
{
check_results=`lcov -v | grep lcov`

if [[ $check_results =~ "lcov" ]]
then
    return
else
    echo "lcov can't be used. Install it"
    exit

fi

}

function usage()
{
  echo "Usage:"
        echo "-h      "
        echo "-all    "
        echo "-add    "
        echo "-init        "
        echo "             "
}

function pilling()
{
        sed -i '/NotifyProcessActive();/i__gcov_flush();' src/gausskernal/process/postmaster/postmaster.cpp
        sed -i 'N;256 i extern "C" void__gcov_flush();' src/gausskernel/process/postmaster/postmaster.cpp

}

function ispipilling_success()
{
 check_result=`git diff src/gausskernel/process/postmaster/postmaster.cpp | grep "+++"`
 if [ [$check_result="+++"] ]; then
         echo "Adding pile dirving function succed"
 else
         echo "ERROR: Adding pile dirving function failed"
 fi

}

function run_schedule()
{
        make fastcheck_single PART=$1;

}


function get_param()
{
 case "$1" in
 -all)
     echo "---------Code Coverage ----------"
     lcov --capture --directory . --output-file coverage.info
     run_schedule()
         ;;
 -add)
     #lcov xxxxxxxxxx
         ;;
 -h)
    usage
         ;;
 -init)
         git clean -dfx
         ./configure --gcc-version=7.3.0 CC=g++ CFLAGS='-O0 -fprofile-arcs -ftest-coverage' --prefix=$GAUSSHOME --3rd=$BINARYLIBS --enable-debug --enable-cassert --enable-thread-safety --with-readline --without-zlib LDFLAGS='-lgcov'
         pilling
         ;;
 *)
         usage
         exit
         ;;
esac
}

function main(){

        lcov_isexits

         get_param $1

}

main $1 $2
